package com.zm.demo.core.concurrent;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.System.out;

/**
 *
 * @author zoum
 * @create 2019/7/1 16:11
 */
public class CyclicBarrierTest {


    public static void main(String[] args) throws Exception{
        //设置一个阻塞点，当所有线程都准备好时，放开阻塞，执行各自的具体逻辑。可循环使用
        CyclicBarrier cyclicBarrier = new CyclicBarrier(19, () -> out.println("车满了，出发了。"));

        ExecutorService pool = Executors.newCachedThreadPool();

        for (int i = 0; i < 40; i++) {
            pool.submit(() -> {
                try {
                    out.println(Thread.currentThread().getName() + "====" + cyclicBarrier.await());
                } catch (Exception e){
                    e.printStackTrace();
                }
            });
        }



        pool.shutdown();
    }
}
